#include <unistd.h>
#include <assert.h>
#include "lock.h"

void lock_init(my_lock *plock) {
	assert(plock);
	plock->val = 0;
}

void lock_lock(my_lock *plock) {
	assert(plock);
	while (__sync_val_compare_and_swap(&plock->val, 0, 1) != 0)
		sleep(0);
}

void lock_unlock(my_lock *plock) {
	assert(plock);
//	assert(__sync_val_compare_and_swap(&plock->val, 1, 0) == 1);
	plock->val--;
}
